﻿using UnityEngine;



[RequireComponent(typeof(MovementComponent))]
public class CrouchComponent : MonoBehaviour
{
    [SerializeField] private float crouchSpeedMultiplier = 0.6f;
    [SerializeField] private float crouchHeight = 1f;

    private MovementComponent _movement;
    private CharacterController _controller;
    private float _originalHeight;
    private bool _isCrouching;

    void Awake()
    {
        _movement = GetComponent<MovementComponent>();
        _controller = GetComponent<CharacterController>();
        _originalHeight = _controller.height;

        GetComponent<InputHandler>().OnCrouchToggled += ToggleCrouch;
    }

    void ToggleCrouch()
    {
        _isCrouching = !_isCrouching;

        if (_isCrouching)
        {
            _controller.height = crouchHeight;
            _movement.AddSpeedModifier(crouchSpeedMultiplier);
        }
        else
        {
            _controller.height = _originalHeight;
            _movement.RemoveSpeedModifier(crouchSpeedMultiplier);
        }
    }
}